using System;
using System.Collections.Generic;

namespace FwkLight.CodeExtensions
{
  public class EnumHelper
  {
    public static List<T> EnumToList<T>()
    {
      Type enumType = typeof(T);

      // Can't use type constraints on value types, so have to do check like this
      if (enumType.BaseType != typeof(Enum))
        throw new ArgumentException("T must be of type System.Enum");

      Array enumValArray = Enum.GetValues(enumType);

      List<T> enumValList = new List<T>(enumValArray.Length);

      foreach (int val in enumValArray)
      {
        enumValList.Add((T)Enum.Parse(enumType, val.ToString()));
      }

      return enumValList;
    }

    public static List<EnumToNameValueItem<T>> EnumToNameValueList<T>()
    {
      Type enumType = typeof(T);

      // Can't use type constraints on value types, so have to do check like this
      if (enumType.BaseType != typeof(Enum))
        throw new ArgumentException("T must be of type System.Enum");

      Array enumValArray = Enum.GetValues(enumType);

      List<EnumToNameValueItem<T>> enumValList = new List<EnumToNameValueItem<T>>(enumValArray.Length);

      foreach (int val in enumValArray)
      {
        enumValList.Add(new EnumToNameValueItem<T>((T)Enum.Parse(enumType, val.ToString()), Enum.Parse(enumType, val.ToString()).ToString()));
      }

      return enumValList;
    }

    public class EnumToNameValueItem<T>
    {
      public EnumToNameValueItem(T value, string name)
      {
        Value = value;
        Name = name;
      }

      public string Name { get; protected set; }
      public T Value { get; protected set; }
    }
  }
}